package com.spider.web.url.https;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.http.client.ClientProtocolException;
import org.xml.sax.InputSource;

public class HttpsTest {
	public static void main(String[] args) {
		String url = "https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4";
		Map<String, String> params = new HashMap();
		try {
			String path="E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\alibaba";
			HttpClientUtils
					.sslPostHTML(
							url,
							params,
							"GBK",
							 ( "E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\alibaba"),
							"changeit",
							 ("E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\pay"),
							"changeit");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		 * try { test(); } catch (HttpException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } catch (IOException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
	}
	
	public static void prasebaba(){

		String url = "https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4";
		Map<String, String> params = new HashMap();
		try {
			String path="E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\alibaba";
			HttpClientUtils
					.sslPostHTML(
							url,
							params,
							"GBK",
							 ( "E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\alibaba"),
							"changeit",
							 ("E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\pay"),
							"changeit");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		 * try { test(); } catch (HttpException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } catch (IOException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
	
	}
private static InputSource getInputSource(String path){
	InputStream stream=null;
	try {
		stream = new FileInputStream( path);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	InputSource is=new InputSource(stream);
	 
	return is; 

}
	// https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4
	// https://is.taobao.org/souren/Default.aspx?&query=xz
	public static void test() throws HttpException, IOException {

		Protocol authhttps = new Protocol(
				"https",

				new AuthSSLProtocolSocketFactory(

						"E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\alibaba",
						"changeit",

						"E:\\Program Files\\MyEclipse\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\security\\pay",
						"changeit"), 443);

		HttpClient client = new HttpClient();

		client.getHostConfiguration().setHost("www.cn.alibaba-inc.com", 443,
				authhttps);

		/* 只能使用相对路径 */

		GetMethod httpget = new GetMethod(
				"/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4");

		int statusCode = client.executeMethod(httpget);
		System.out.println(statusCode);
		System.out.println(httpget.getResponseBodyAsString());
	}

	public static void DownLoadPagesPay(String urlStr, String outPath) {
		int chByte = 0;
		URL url = null;
		HttpURLConnection httpConn = null;
		InputStream in = null;
		FileOutputStream out = null;

		try {

			url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			HttpURLConnection.setFollowRedirects(true);
			httpConn.setRequestMethod("GET");
			httpConn.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows xp)");
			// System.out.print(httpConn.getResponseMessage()
			// + httpConn.getResponseCode());
			in = httpConn.getInputStream();
			out = new FileOutputStream(new File(outPath));

			chByte = in.read();
			while (chByte != -1) {
				out.write(chByte);
				chByte = in.read();
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
				httpConn.disconnect();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}
}
